# !/usr/bin/env python3
# -*- coding: utf-8 -*-

import urllib2

url = "http://www.itcast.cn"

# IE 9.0 的 User-Agent
header = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"}
request = urllib2.Request(url, headers=header)

# 也可以通过调用Request.add_header() 添加/修改一个特定的header
request.add_header("Connection", "keep-alive")

# 也可以通过调用Request.get_header()来查看header信息
print(request.get_header(header_name="Connection"))  # keep-alive

response = urllib2.urlopen(request)

print response.code  # 可以查看响应状态码
print(response.geturl())  # http://www.itcast.cn，响应的url，防止重定向
print(response.info())  # 服务器响应的HTTP报头
# Server: Tengine
# Content-Type: text/html
# Date: Mon, 02 Apr 2018 10:37:48 GMT
# Accept-Ranges: bytes
# Via: cache13.l2st3-1[86,200-0,M], cache48.l2st3-1[87,0], kunlun9.cn289[231,200-0,M], kunlun6.cn289[233,0]
# X-Cache: MISS TCP_MISS dirn:-2:-2 mlen:-1
# X-Swift-SaveTime: Mon, 02 Apr 2018 10:37:48 GMT
# X-Swift-CacheTime: 0
# Timing-Allow-Origin: *
# EagleId: 75ac070615226654681773194e
# Transfer-Encoding: chunked
# Proxy-Connection: Close
html = response.read()

print html

# 请求
# --------------------------------------------------------------------------------------------------------------------------------------------------------------
# GET / HTTP/1.1
# Accept-Encoding: identity
# Host: www.itcast.cn
# Connection: close
# User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
